const {handleCount} = require("../../utils/index")
Page({
    data:{
        playlists:[]
    },
    onLoad:function(){
        wx.request({
            url: 'http://192.168.4.18:3000/top/playlist?cat=华语&limit=15',
            header: {'content-type':'application/json'},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result)=>{
                this.handleHttpData(result)
            }
        });
    },
    handleHttpData(result){
        let playlists = [...this.data.playlists,...result.data.playlists]
        playlists.forEach(item=>{
            item.playCount = handleCount(item.playCount)
        })
        this.setData({
            playlists
        })
    },
    handleClick(event){
        let {id} = event.currentTarget.dataset
        wx.navigateTo({
            url: `/pages/detail/detail?id=${id}`
        });
    },
    onReachBottom(){
        let offset = this.data.playlists.length;
        let url=`http://192.168.4.18:3000/top/playlist?cat=华语&limit=15&offset=${offset}`
        wx.request({
            url,
            data: {},
            header: {'content-type':'application/json'},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result)=>{
                this.handleHttpData(result)
            }
        });
    }
})
